package utwente.hmi.touchmol.elements;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.util.LinkedList;
import java.util.ListIterator;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.border.TitledBorder;

import utwente.hmi.touchmol.*;

/**
 * The queue panel shows the queue, and allows the moderator to assign a turn with it.
 * @author Jeroen Logtenberg
 */
public class QueuePanel extends ButtonPanel {
	
	private static final long serialVersionUID = 1L;
	
	private Dimension currentSize;
	private int moderator;
	private LinkedList<UserButton> queue;
	
	/**
	 * Makes a new queue panel.
	 * @param width Width in pixels
	 * @param height Height in pixels
	 * @param moderator The user allowed to operate the panel. Use TouchMolGUI.USER_[COLOR].
	 */
	public QueuePanel(int width, int height, int moderator){
		queue = new LinkedList<UserButton>();
		this.moderator = moderator;
		
		currentSize = new Dimension(width, height);
		setSize(currentSize);
		setPreferredSize(currentSize);
		setBackground(Color.BLACK);

		setVisible(true);
		BoxLayout box = new BoxLayout(this,BoxLayout.Y_AXIS);
		setLayout(box);
			
	}
	
	public int getModerator(){
		return moderator;
	}
	
	private void updateDisplay(){
		int size = queue.size();
		int h = UserButton.defaultHeight;
		int panelHeight = currentSize.height;

		this.removeAll();
		
		Dimension fill = new Dimension(0, (panelHeight - (size * 2 - 1) * h) / 2);
		add(Box.createRigidArea(fill));

		UserButton b;
		for(int i = 0; i < size; i++){
			b = queue.get(i);
			String title = Integer.toString(i + 1);
			TitledBorder t = BorderFactory.createTitledBorder(UserButton.BORDER_QUEUE, title);
			b.setBorder(t);
			b.setVisible(true);
			add(b);
			if(i < size - 1) 
				add(Box.createVerticalGlue());
		}
		
		add(Box.createRigidArea(fill));
		this.validate(); //redraw layout.
		this.repaint();
	}

	@Override
	public UserButton buttonTouched(Point location) {
		ListIterator<UserButton> it = queue.listIterator();
		UserButton b;
		//TouchMolGUI.debug("Checking for touch on " + location);
		while(it.hasNext()){
			b = it.next();
			if(b.buttonTouched(location)) {
				return b;
			}
		}
		return null;
	}

	@Override
	public void releaseTurn(UserButton user) {
		ListIterator<UserButton> it = queue.listIterator();
		UserButton b;
		while(it.hasNext()){
			b = it.next();
			if(b.getUserID() == user.getUserID()){
				it.remove();
				b.setBorder(null);
				TouchMolGUI.debug("User dequeued.");
				updateDisplay();
			} else{
				TouchMolGUI.debug("User was not in the queue, so not dequeued.");
			}
		}
		
	}

	@Override
	public void requestTurn(UserButton user) {
		if(queue.contains(user)){
			TouchMolGUI.debug("User already in the queue, not queuing again.");
		} else{
			queue.add(user); //adds element to the end of the queue.
			updateDisplay();
			TouchMolGUI.debug("User queued.");
		}
	}

	@Override
	public void takeTurn(UserButton user) {
		ListIterator<UserButton> it = queue.listIterator();
		UserButton b;
		while(it.hasNext()){
			b = it.next();
			if(b.getUserID() == user.getUserID()){
				it.remove();
				b.setBorder(UserButton.BORDER_TURN);
				TouchMolGUI.debug("User dequeued.");
				updateDisplay();
			} else{
				TouchMolGUI.debug("User was not in the queue, so not dequeued.");
			}
		}
		
	}
}
